<?php

$_SESSION['site'] = array('menu', 'feedback');

postFeedbackButtonLeiste ();

if (isset($_POST['speichernFeedback'])) {
    if ($_POST['id']) {
        $query = mysql_query('UPDATE Feedback set Kunde = ' . $_POST['comboboxKunde'] . ', Thema = ' . $_POST['thema'] . ', Beschreibung="' . $_POST['beschreibung'] . '", Eintragungsdatum="' . $_POST['eintragungsdatum'] . '" where Feedback.ID = ' . $_POST['id']);
        
        if ($query) {
            echo "<div class='info'>Feedback erfolgreich ge&auml;ndert!</div>";
            postFeedbackListe ();
            // TODO: Fehlerbehandlung
        } else {
            echo "<div class='error'>Feedback konnte nicht ge&auml;ndert werden!</div>";
        }
    } else {
        $query = mysql_query('INSERT into Feedback values (null,' . $_POST['comboboxKunde'] . ', ' . $_POST['thema'] . ', "' . $_POST['beschreibung'] . '", "' . $_POST['eintragungsdatum'] . ' ")');
        if ($query) {
            echo "<div class='info'>Feedback erfolgreich angelegt!</div>";
            postFeedbackListe ();
            // TODO: Fehlerbehandlung
        } else {
            echo "<div class='error'>Feedback konnte nicht angelegt werden!</div>";
        }
    }

} elseif (isset($_POST['neuesFeedback'])) {
    postFeedback (array());

} elseif (isset($_POST['detailsFeedback'])) {
    $feedback = $_POST['detailsFeedback'];
    postFeedbackDetails ($feedback);

} elseif (isset($_POST['sucheFeedback'])) {
    $string = 'SELECT *, Feedback.ID as FeedbackID, Kunde.ID as KundeID, Thema.ID as ThemaID from Feedback
        join Kunde on Feedback.Kunde = Kunde.ID join Thema on Feedback.Thema = Thema.ID
        where Beschreibung like "%' . $_POST['suche'] . '%" or concat(Kunde.Vorname," ",Kunde.Nachname) like "%' . $_POST['suche'] . '%" or Bezeichnung like "%' . $_POST['suche'] . '%" 
        or Firma like "%' . $_POST['suche'] . '%"
        order by Feedback.ID asc';
    $query = mysql_query($string);
    
    if (mysql_num_rows ($query) > 0) {
        echo '<div class="info">Suchergebnisse f&uuml;r "' . $_POST['suche'] . '"</div>';
    } else {
        echo '<div class="error">Keine Ergebnisse gefunden f&uuml;r "' . $_POST['suche'] . '"</div>';
    }

    postFeedbackTabelle ($query);
}elseif (isset($_POST['analyseFeedback'])) {
    include ('feedback_auswerten.php');

} else {

    if (isset($_POST['aendernFeedback'])) {
        $feedback = $_POST['aendernFeedback'];
        postFeedbackBearbeiten ($feedback);
    } elseif (isset($_POST['resetid'])) {
        $feedback = $_POST['resetid'];
        postFeedbackBearbeiten ($feedback);
    } else {
        postFeedbackListe ();
    }
}

function postFeedbackListe () {
    $query = mysql_query('SELECT *, Feedback.ID as FeedbackID, Kunde.ID as KundeID, Thema.ID as ThemaID from Feedback
        join Kunde on Feedback.Kunde = Kunde.ID join Thema on Feedback.Thema = Thema.ID order by Feedback.ID asc');
    
    postFeedbackTabelle ($query);
}

function postFeedbackButtonLeiste () {
    echo '<div class="buttonleiste">';

    if (isset($_POST['neuesFeedback']) || isset($_POST['aendernFeedback']) || isset($_POST['resetid'])) {
        if (isset($_POST['aendernFeedback'])) {
            $id = $_POST['aendernFeedback'];
        } elseif (isset($_POST['resetid'])) {
            $id = $_POST['resetid'];
        }

        echo '<a href="index.php?section=feedback"><button class="submit left r10" name="feedback" type="submit">Zur Feedback&uuml;bersicht</button></a>';
        echo '<form action="index.php?section=feedback" method="post">
                <input type="hidden" name="resetid" value="' . $id . '"> 
                <button class="submit left r10">Reset</button>
            </form>';
        echo '<button class="submit left buttonIEdisable" name="speichernFeedback" type="submit" form="feedbackform">Speichern</button>';
   
    } elseif (isset($_POST['detailsFeedback'])) {
        $id = $_POST['detailsFeedback'];

        echo '<a href="index.php?section=feedback"><button class="submit left r10" name="alleFeedbacks" type="submit">Alle Feedbacks anzeigen</button></a>';
        echo '<form action="index.php?section=feedback" method="post">
                <input type="hidden" name="aendernFeedback" value="' . $id . '"> 
                <button class="submit left">&Auml;ndern</button>
            </form>';
    } elseif (isset($_POST['analyseFeedback'])) {
         echo '<a href="index.php?section=feedback"><button class="submit left r10" name="alleFeedbacks" type="submit">Alle Feedbacks anzeigen</button></a>';
         echo '<button class="submit left buttonIEdisable" name="auswerten_feedback" onclick="drawDiagramm();" type="button" form="feedbackanalyse">Auswerten</button>';
   

    
    } else {
        if (isset($_POST['sucheFeedback'])) {
            echo '<a href="index.php?section=feedback"><button class="submit left r10" name="alleFeedbacks" type="submit">Alle Feedbacks anzeigen</button></a>';
        } 

        echo '<form method="post" action="index.php?section=feedback" class="left r10">
            <button class="submit" name="neuesFeedback" type="submit">Feedback hinzuf&uuml;gen</button>
        </form>';

        echo '<a href="index.php?section=menu"><button class="submit left r10" name="menu" type="submit">Zum Men&uuml;</button></a>';
        if (!isset($_POST['sucheFeedback'])) {
            echo '<form method="post" action="index.php?section=feedback" class="left r10">
                <button class="submit" name="analyseFeedback" type="submit">Feedback auswerten</button>
            </form>';
        }
        echo '<form method="post" action="index.php?section=feedback" id="searchFeedback" class="right">
            <input class="suche" name="suche" placeholder="Suche" pattern=".+" type="search" />
            <button class="submit" name="sucheFeedback" type="submit">Suche</button>
        </form>';
    }
    echo '</div>';
}

function postFeedbackTabelle ($query) {
    echo '<table class="liste clear">
        <tr>
            <th>#</th>
            <th>Kunde</th>
            <th>Eintragungsdatum</th>
            <th>Thema</th>
            <th>Beschreibung</th>
            <th>&Auml;ndern</th>
        </tr>';
    for ($i=0; $i < mysql_num_rows ($query); $i++) { 
        $row = mysql_fetch_array($query);
        if ($i % 2 == 0) {
            echo '<tr>';
        } else {
            echo '<tr class="farbigeZeile">';
        }

        echo '<td>
                    <form action="index.php?section=feedback" method="post">
                        <input type="hidden" name="detailsFeedback" value="' . $row['FeedbackID'] . '"> 
                        <button class="linkbutton">' . $row['FeedbackID'] . '</button>
                    </form>
                </td>
                <td>';
                    if ($row['Firma'] == null) {
                        echo $row['Vorname'] . ' ' . $row['Nachname'];
                    } else {
                        echo $row['Firma'];
                    }
                echo '</td>
                <td>' . $row['Eintragungsdatum'] . '</td>
                <td>' . $row['Bezeichnung'] . '</td>
                <td>' . $row['Beschreibung'] . '</td>
                <td>
                    <form action="index.php?section=feedback" method="post">
                        <input type="hidden" name="aendernFeedback" value="' . $row['FeedbackID'] . '"> 
                        <button class="linkbutton">&Auml;ndern</button>
                    </form>
                </td>
            </tr>';
    }
    echo '</table>';
}

function postFeedbackBearbeiten ($feedback) {
    $query = mysql_query('SELECT *, Feedback.ID as FeedbackID, Kunde.ID as KundeID, Thema.ID as ThemaID from Feedback
    join Kunde on Feedback.Kunde = Kunde.ID join Thema on Feedback.Thema = Thema.ID where Feedback.ID = ' . $feedback);    
    $row = mysql_fetch_array($query);
    postFeedback($row);
}

function postFeedback ($row) {
    if(!isset($row['Eintragungsdatum'])) $row['Eintragungsdatum'] = date("Y-m-d",time());
    echo '
        <form class="formular" action="index.php?section=feedback" method="post" id="feedbackform" name="feedbackform">
        <ul>
            <li>
                 <h2>Feedback ';
    
    if (isset($row['ID'])) {
        echo '#' . $row['FeedbackID'] . ' &auml;ndern';
    } else {
        echo 'hinzuf&uuml;gen';
    }
                
    echo '</h2>
                 <span class="required_notification">* Ben&ouml;tigte Felder</span>
            </li>
            <li>
                <label for="comboboxKunde_input">Kunde:</label>';
    include ('inc/comboboxKunde.php');
    echo'
        <li>
            <label for="eintragungsdatum">Eintragungsdatum:</label>
            <input id="eintragungsdatum" type="date" name="eintragungsdatum" placeholder="YYYY-MM-DD" value="' . $row['Eintragungsdatum'] . '" required>
        </li>
        </li>
             <li>
                <label for="thema">Thema:</label>
                <select id="thema" name="thema" size="1" required>';
    $result = mysql_query ("select * from Thema");
    $zeilenanzahl = mysql_num_rows ($result);
    echo '<option value=""></option>';
    for ($i = 1; $i <= $zeilenanzahl; $i++) {
        $thema = mysql_fetch_assoc($result);
        if ($row['Bezeichnung'] == $thema['Bezeichnung']) {
            echo '<option value="' . $thema['ID'] . '" selected="selected">' . $thema['Bezeichnung'] . '</option>';
        } else {
            echo '<option value="' . $thema['ID'] . '">' . $thema['Bezeichnung'] . '</option>';
        }
    }
    echo '
                </select>
            </li>
            <li>
                <label for="beschreibung">Beschreibung:</label>
                <textarea id="beschreibung" name="beschreibung" placeholder="Beschreibung eintragen" maxlength="300" required>' . $row['Beschreibung'] . '</textarea>
                <input type="hidden" name="id" value="' . $row['FeedbackID'] . '"> 
            </li>
            <li>
                <button class="submit buttonIEenable" name="speichernFeedback" type="submit">Speichern</button>
            </li>
        </ul>
    </form>
    ';
}

function postFeedbackDetails ($feedback) {
    $query = mysql_query('SELECT *, Feedback.ID as FeedbackID, Kunde.ID as KundeID, Thema.ID as ThemaID from Feedback
        join Kunde on Feedback.Kunde = Kunde.ID join Thema on Feedback.Thema = Thema.ID where Feedback.ID = ' . $feedback);    
    $row = mysql_fetch_array($query);

    echo '
    <table class="detailliste">
        <tr>
            <td colspan="2"><h2>Feedback #' . $row['FeedbackID'] . '</h2></td>
        </tr>
        <tr>
            <td class="beschriftung">Kunde:</td>
            <td>';
             if ($row['Firma'] == null) {
                 echo $row['Vorname'] . ' ' . $row['Nachname'];
             } else {
                 echo $row['Firma'];
             }
         echo 
         '</td>
        </tr>
        <tr>
            <td class="beschriftung">Thema:</td>
            <td>' . $row['Bezeichnung'] . '</td>
        </tr>
        <tr>
            <td class="beschriftung">Beschreibung:</td>
            <td>' . $row['Beschreibung'] . '</td>
        </tr>
    </table>';
}

?>